package com.songlanyun.modules.opus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.Query;

import com.songlanyun.modules.opus.dao.OpusThemeDao;
import com.songlanyun.modules.opus.entity.OpusTheme;
import com.songlanyun.modules.opus.service.OpusThemeService;
import org.springframework.transaction.annotation.Transactional;


@Service("opusThemeService")
public class OpusThemeServiceImpl extends ServiceImpl<OpusThemeDao, OpusTheme> implements OpusThemeService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OpusTheme> page = this.page(
                new Query<OpusTheme>().getPage(params),
                new QueryWrapper<OpusTheme>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveOpusTheme(Long oid, List<Long> themeIds) {
        themeIds.forEach(t -> {
            OpusTheme opusTheme = new OpusTheme();
            opusTheme.setOpusId(oid);
            opusTheme.setThemeId(t);
            this.baseMapper.insert(opusTheme);
        });
    }

    @Override
    public void removeByOid(Long oid) {
        this.baseMapper.delete(
                new LambdaQueryWrapper<OpusTheme>()
                        .eq(OpusTheme::getOpusId, oid)
        );
    }

    @Override
    public void removeByTid(Integer tid) {
        this.baseMapper.delete(
                new LambdaQueryWrapper<OpusTheme>()
                        .eq(OpusTheme::getThemeId, tid)
        );
    }

    @Override
    public List<OpusTheme> listByOpusId(Long oid) {

        List<OpusTheme> opusThemes = this.baseMapper.selectList(
                new LambdaQueryWrapper<OpusTheme>()
                        .eq(OpusTheme::getOpusId, oid)
        );
        return opusThemes;
    }

    @Override
    public Integer opusNum(Long tid) {
        return this.baseMapper.opusNum(tid);
    }


}